//
//  RMMOrderDeatilHeadView.m
//  Romomo
//
//  Created by puman on 2016/10/24.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMOrderDeatilHeadView.h"

@implementation RMMOrderDeatilHeadView
{
    UIView *_backView;
    UILabel *_label;
    UILabel *_timeLabel;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = kWhiteColor;
        _label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 150, 44)];
        _label.text = @"订单状态";
        _label.textColor = COLORFROMCODE(0x4A4A4A, 1);
        _label.font = kFont(13);
        _label.numberOfLines = 0;
        [self addSubview:_label];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_label.right, 0, self.width - _label.right - 15, 44)];
        _timeLabel.textColor = COLORFROMCODE(0x9B9B9B, 1);
        _timeLabel.font = kFont(12);
        _timeLabel.numberOfLines = 0;
        _timeLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:_timeLabel];
        
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, _label.bottom, self.width, 76)];
        [self addSubview:_backView];

        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, self.width - 30, 1)];
        lineLabel.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
        [_backView addSubview:lineLabel];
        
        
        CGFloat width = (self.width - 300) / 3;
        NSArray *array = @[@"已支付",@"待服务",@"服务中",@"已完成"];
        for (int i = 0; i < 4; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.frame = CGRectMake(30 + (60 + width) * i, lineLabel.bottom + 15 , 60, 24);
          //  button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 12;
            button.userInteractionEnabled = NO;
            [button setTitleColor:COLORFROMCODE(0xD7D7D7, 1) forState:(UIControlStateNormal)];
            [button setTitle:array[i] forState:(UIControlStateNormal)];
            button.tag = 10 + i;
            button.titleLabel.font = kFont(12);
            [_backView addSubview:button];
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(button.center.x - 6, button.bottom - 3, 12, 8)];
            imageView.tag = 20 + i;
            imageView.hidden = YES;
            imageView.image = [UIImage imageNamed:@"moredown@3x"];
            [_backView addSubview:imageView];
            
            UIImageView *imageViews = [[UIImageView alloc] initWithFrame:CGRectMake(button.center.x - 5, button.bottom + kRow, kRow, kRow)];
            imageViews.tag = 30 + i;
            imageViews.backgroundColor = COLORFROMCODE(0xD8D8D8 , 1);
            imageViews.layer.cornerRadius = 5;
            //imageViews.layer.masksToBounds = YES;
            [_backView addSubview:imageViews];
            
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(imageViews.right + 2, imageViews.top + 4, 60 + width - kRow - 4, 2)];
            lineView.backgroundColor = COLORFROMCODE(0xD8D8D8 , 1);
            lineView.tag = 40 + i;
            if (i == 3) {
                lineView.hidden = YES;
            }
            [_backView addSubview:lineView];
        }
    }
    return self;
}
- (void)setTypeName:(NSString *)typeName{
    NSString *isHome = nil;
    if ([self.isHome isEqualToString:@"1"]) {
        isHome = @"上门服务";
    } else {
        isHome = @"到店服务";
    }
    if ([typeName isEqualToString:@"退款中"] || [typeName isEqualToString:@"退款完成"] || [typeName isEqualToString:@"已完成"] || [typeName isEqualToString:@"已取消"]) {
        _label.text = [NSString stringWithFormat:@"订单状态\n%@",[NSString stringWithFormat:@"%@ %@",self.time,isHome]];
        [self changeLabelTextColorOneWithLabel:_label forRange:NSMakeRange(4, _label.text.length - 4) byColor:COLORFROMCODE(0x9B9B9B, 1)];
        _timeLabel.text = typeName;
        _backView.hidden = YES;
        if ([typeName isEqualToString:@"退款中"]) {
            _timeLabel.textColor = COLORFROMCODE(0xF95B45, 1);
        } else if([typeName isEqualToString:@"退款完成"]){
            _label.text = [NSString stringWithFormat:@"订单状态\n%@",[NSString stringWithFormat:@"到账时间:%@",self.time]];
        } else{
            _timeLabel.textColor = COLORFROMCODE(0x9B9B9B, 1);
        }
    } else {
        _backView.hidden = NO;
        _label.text = @"订单状态";
        _timeLabel.text = [NSString stringWithFormat:@"%@ %@",self.time,isHome];
    }
}
- (void)changeLabelTextColorOneWithLabel:(UILabel *)label forRange:(NSRange)range byColor:(UIColor *)color{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
    [attributedString addAttribute:NSFontAttributeName value:kFont(10) range:range];
    [label setAttributedText:attributedString];
}

- (void)setType:(NSString *)type{
    if ([type integerValue] >= 4) {
        _backView.hidden = YES;
    } else {
        _backView.hidden = NO;
    }
    NSArray *array = @[@"待支付",@"待服务",@"服务中",@"已完成"];
    for (int i = 0; i < 4; i++) {
        UIButton *button = [self viewWithTag:10 + i];
        button.backgroundColor = kWhiteColor;
        [button setTitleColor:COLORFROMCODE(0xD7D7D7, 1) forState:(UIControlStateNormal)];
        [button setTitle:array[i] forState:(UIControlStateNormal)];
        UIImageView *imageV = [self viewWithTag:20 + i];
        imageV.hidden = YES;
        UIImageView *view = [self viewWithTag:30 + i];
        view.backgroundColor = COLORFROMCODE(0xD8D8D8 , 1);
        UIView *views = [self viewWithTag:40 + i];
        views.backgroundColor = COLORFROMCODE(0xD8D8D8 , 1);
        if (i == [type intValue]) {
            imageV.hidden = NO;
        }
        if ([type isEqualToString:@"0"]) {
            if (i == 0) {
                view.backgroundColor = RGBA(228, 157, 56, 1);
                button.backgroundColor = RGBA(228, 157, 56, 1);
                [button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
                [button setTitle:@"待支付" forState:(UIControlStateNormal)];
            }
        } else {
            if (i < [type intValue]) {
                button.backgroundColor = kWhiteColor;
                [button setTitleColor:kBasicColor forState:(UIControlStateNormal)];
                views.backgroundColor = kBasicColor;
                view.backgroundColor = kBasicColor;
                if (i == 0) {
                    [button setTitle:@"已支付" forState:(UIControlStateNormal)];
                }
            } else if (i == [type intValue]) {
                button.backgroundColor = kBasicColor;
                [button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];

                view.backgroundColor = kBasicColor;
                if (i == 0) {
                    [button setTitle:@"已支付" forState:(UIControlStateNormal)];
                }
                if (i == 3) {
                    [button setTitle:@"待评价" forState:(UIControlStateNormal)];
                }
            }
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
